import re
import json
from bs4 import BeautifulSoup
from .base_adapter import BaseAdapter

class ZhihuAdapter(BaseAdapter):
    _SALT_API = "https://zhihu-api.example.com/salt"
    
    def detect(self, url):
        return re.match(r"^https?://www\.zhihu\.com/", url)

    def parse(self, html):
        soup = BeautifulSoup(html, 'lxml')
        return {
            'title': self._parse_title(soup),
            'author': self._parse_author(soup),
            'is_salt': self._check_salt(soup),
            'content': self._parse_content(soup)
        }

    def _parse_title(self, soup):
        return soup.find('h1', {'class': 'Post-Title'}).text.strip()

    def _check_salt(self, soup):
        return bool(soup.find('div', class_='salt-content'))

    def download(self):
        if self.requires_salt_unlock():
            return self._unlock_salt_content()
        return super().download()

    def _unlock_salt_content(self):
        # 实现盐选解锁逻辑
        headers = self._gen_mobile_headers()
        response = requests.get(self._SALT_API, headers=headers)
        return response.json()['content']